<?php
require_once '../config/database.php';

class Order {
    private $conn;
    private $table_name = "orders";

    public $id;
    public $order_number;
    public $table_number;
    public $customer_name;
    public $customer_phone;
    public $items;
    public $total_amount;
    public $tax_amount;
    public $grand_total;
    public $status;
    public $created_at;
    public $completed_at;

    public function __construct() {
        $database = new Database();
        $this->conn = $database->getConnection();
    }

    // 创建订单
    public function create() {
        // 生成订单号
        $this->order_number = 'ORD' . date('YmdHis') . rand(100, 999);
        
        $query = "INSERT INTO " . $this->table_name . " 
                 (order_number, table_number, customer_name, customer_phone, items, 
                  total_amount, tax_amount, grand_total, status) 
                 VALUES (:order_number, :table_number, :customer_name, :customer_phone, :items, 
                         :total_amount, :tax_amount, :grand_total, :status)";
        
        $stmt = $this->conn->prepare($query);
        
        $items_json = json_encode($this->items);
        
        $stmt->bindParam(':order_number', $this->order_number);
        $stmt->bindParam(':table_number', $this->table_number);
        $stmt->bindParam(':customer_name', $this->customer_name);
        $stmt->bindParam(':customer_phone', $this->customer_phone);
        $stmt->bindParam(':items', $items_json);
        $stmt->bindParam(':total_amount', $this->total_amount);
        $stmt->bindParam(':tax_amount', $this->tax_amount);
        $stmt->bindParam(':grand_total', $this->grand_total);
        $stmt->bindParam(':status', $this->status);

        if ($stmt->execute()) {
            $this->id = $this->conn->lastInsertId();
            return $this->id;
        }
        return false;
    }

    // 获取所有订单
    public function getAll() {
        $query = "SELECT * FROM " . $this->table_name . " ORDER BY created_at DESC";
        $stmt = $this->conn->prepare($query);
        $stmt->execute();
        
        $orders = $stmt->fetchAll(PDO::FETCH_ASSOC);
        
        // 解析items JSON
        foreach ($orders as &$order) {
            $order['items'] = json_decode($order['items'], true);
        }
        
        return $orders;
    }

    // 按状态获取订单
    public function getByStatus($status) {
        $query = "SELECT * FROM " . $this->table_name . " WHERE status = :status ORDER BY created_at DESC";
        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(':status', $status);
        $stmt->execute();
        
        $orders = $stmt->fetchAll(PDO::FETCH_ASSOC);
        
        foreach ($orders as &$order) {
            $order['items'] = json_decode($order['items'], true);
        }
        
        return $orders;
    }

    // 获取单个订单详情
    public function getById($id) {
        $query = "SELECT * FROM " . $this->table_name . " WHERE id = :id";
        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(':id', $id);
        $stmt->execute();
        
        $order = $stmt->fetch(PDO::FETCH_ASSOC);
        
        if ($order) {
            $order['items'] = json_decode($order['items'], true);
        }
        
        return $order;
    }

    // 按订单号获取订单
    public function getByOrderNumber($order_number) {
        $query = "SELECT * FROM " . $this->table_name . " WHERE order_number = :order_number";
        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(':order_number', $order_number);
        $stmt->execute();
        
        $order = $stmt->fetch(PDO::FETCH_ASSOC);
        
        if ($order) {
            $order['items'] = json_decode($order['items'], true);
        }
        
        return $order;
    }

    // 更新订单状态
    public function updateStatus($id, $status) {
        $query = "UPDATE " . $this->table_name . " SET status = :status";
        
        if ($status == 'completed') {
            $query .= ", completed_at = NOW()";
        } else {
            $query .= ", completed_at = NULL";
        }
        
        $query .= " WHERE id = :id";
        
        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(':status', $status);
        $stmt->bindParam(':id', $id);
        
        return $stmt->execute();
    }

    // 删除订单
    public function delete($id) {
        $query = "DELETE FROM " . $this->table_name . " WHERE id = :id";
        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(':id', $id);
        
        return $stmt->execute();
    }

    // 获取今日订单统计
    public function getTodayStats() {
        $query = "SELECT 
                 COUNT(*) as total_orders,
                 SUM(grand_total) as total_revenue,
                 AVG(grand_total) as avg_order_value,
                 status,
                 COUNT(*) as status_count
                 FROM " . $this->table_name . " 
                 WHERE DATE(created_at) = CURDATE()
                 GROUP BY status";
        
        $stmt = $this->conn->prepare($query);
        $stmt->execute();
        
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }

    // 获取桌号订单
    public function getByTableNumber($table_number) {
        $query = "SELECT * FROM " . $this->table_name . " 
                 WHERE table_number = :table_number 
                 AND status NOT IN ('completed', 'cancelled')
                 ORDER BY created_at DESC";
        
        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(':table_number', $table_number);
        $stmt->execute();
        
        $orders = $stmt->fetchAll(PDO::FETCH_ASSOC);
        
        foreach ($orders as &$order) {
            $order['items'] = json_decode($order['items'], true);
        }
        
        return $orders;
    }
}
?>